package grammars;
import java.io.*;
class TopDownParsing {
    boolean transduce(Reader in, Writer out) throws IOException {
        int ch = in.read();
        if (ch=='h') { out.write(ch); return true; }
        if (ch=='g') { out.write('a'); return transduce(in,out); }
        return false;
    }
    public static void main(String[] args) {
        Reader r = new StringReader("hgggggghi");
        Writer w = new PrintWriter(System.out);
        TopDownParsing t = new TopDownParsing();
        try { while (true) { if (!t.transduce(r,w)) break; w.flush(); } }
        catch (IOException e) { System.out.println("Died "+e);
        }

    }
}